package com.wingsfrog.tabs.app;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.*;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements View.OnClickListener {

    private ViewPager viewPager;
    private FragmentPagerAdapter fragmentPagerAdapter;
    private List<Fragment> fragmentList = new ArrayList<Fragment>();


    private LinearLayout tab1;
    private LinearLayout tab2;
    private LinearLayout tab3;
    private ImageView tabiv1;
    private ImageView tabiv2;
    private ImageView tabiv3;
    private TextView tabtxt1;
    private TextView tabtxt2;
    private TextView tabtxt3;
    private TextView header_middle_tv;
    private ImageButton header_left_ibtn;
    private ImageButton header_right_ibtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setContentView(R.layout.activity_main);
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setNavigationBarTintEnabled(true);
        tintManager.setTintColor(getResources().getColor(R.color.theme_blue));
        viewPager = (ViewPager) findViewById(R.id.mainViewPager);

        initView();

        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragmentList.get(i);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };

        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            private int currentIndex;

            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                resetTabStatus();
                switch (position) {
                    case 0:
                        tabiv1.setImageResource(R.drawable.index_on);
                        tabtxt1.setTextColor(getResources().getColor(R.color.theme_blue));
                        header_middle_tv.setText(R.string.home);
                        header_left_ibtn.setVisibility(View.VISIBLE);
                        header_right_ibtn.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        tabiv2.setImageResource(R.drawable.order_on);
                        tabtxt2.setTextColor(getResources().getColor(R.color.theme_blue));
                        header_middle_tv.setText(R.string.order);
                        header_left_ibtn.setVisibility(View.INVISIBLE);
                        header_right_ibtn.setVisibility(View.INVISIBLE);
                        break;
                    case 2:
                        tabiv3.setImageResource(R.drawable.my_on);
                        tabtxt3.setTextColor(getResources().getColor(R.color.theme_blue));
                        header_middle_tv.setText(R.string.myself);
                        header_left_ibtn.setVisibility(View.INVISIBLE);
                        header_right_ibtn.setVisibility(View.INVISIBLE);
                        break;
                }

                currentIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });



    }


    protected void resetTabStatus() {
        tabiv1.setImageResource(R.drawable.index_off);
        tabiv2.setImageResource(R.drawable.order_off);
        tabiv3.setImageResource(R.drawable.my_off);
        tabtxt1.setTextColor(getResources().getColor(R.color.light_black));
        tabtxt2.setTextColor(getResources().getColor(R.color.light_black));
        tabtxt3.setTextColor(getResources().getColor(R.color.light_black));
    }

    private void initView() {
        header_middle_tv = (TextView) findViewById(R.id.header_middle_tv);
        tab1 = (LinearLayout) findViewById(R.id.tab1);
        tab2 = (LinearLayout) findViewById(R.id.tab2);
        tab3 = (LinearLayout) findViewById(R.id.tab3);
        tabiv1 = (ImageView) findViewById(R.id.tab1_iv);
        tabiv2 = (ImageView) findViewById(R.id.tab2_iv);
        tabiv3 = (ImageView) findViewById(R.id.tab3_iv);
        tabtxt1 = (TextView) findViewById(R.id.tab1_txt);
        tabtxt2 = (TextView) findViewById(R.id.tab2_txt);
        tabtxt3 = (TextView) findViewById(R.id.tab3_txt);
        header_left_ibtn = (ImageButton) findViewById(R.id.header_left_ibtn);
        header_right_ibtn = (ImageButton) findViewById(R.id.header_right_ibtn);


        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);


        MainTab01 tab01 = new MainTab01();
        MainTab02 tab02 = new MainTab02();
        MainTab03 tab03 = new MainTab03();

        fragmentList.add(tab01);
        fragmentList.add(tab02);
        fragmentList.add(tab03);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tab1:
                viewPager.setCurrentItem(0, false);
                break;
            case R.id.tab2:
                viewPager.setCurrentItem(1, false);
                break;
            case R.id.tab3:
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,CameraActivity.class);
                startActivity(intent);
//                viewPager.setCurrentItem(2, false);
                break;
        }
    }


}
